<template>
  <div class="app-container">
    <el-form style="display: flex; justify-content: flex-end">
      <el-form-item >
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >刷新</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="tableData"
    >
      <el-table-column label="标题"  prop="title" :min-width="120"
                       :max-width="600" />
      <el-table-column prop="fileType" label="文件类型" width="120">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mc_file_type"
            :value="scope.row.fileType"
          />
        </template>
      </el-table-column>
      <el-table-column label="产品类型"  prop="productType" width="200">
        <template slot-scope="scope">
<!--          {{ dict.type.mc_product_type.find(v => v.value == scope.row.productType).label }}-->
          <dict-tag
            :options="dict.type.mc_product_type"
            :value="scope.row.productType"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否公开"  prop="isOpen" width="120">
        <template slot-scope="scope">
          {{scope.row.isOpen === 0 ? '不公开':'公开' }}
        </template>
      </el-table-column>
      <el-table-column label="发布状态"  prop="versionDesc" width="120">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.review_status"
            :value="scope.row.reviewState"
          />
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="createTime" width="200">
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d}  {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="修改时间" align="center" prop="modifyTime" /> -->
      <el-table-column
        label="操作"
        align="center"
        width="180"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleDownload(scope.row)"
          >下载</el-button
          >
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-download"-->
<!--            @click="handleDownload(scope.row)"-->
<!--          >下载</el-button-->
<!--          >-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleModify(scope.row)"
          >修改</el-button
          >
          <el-button
          size="mini"
          type="text"
          icon="el-icon-delete"
          v-hasPermi="['official:swiper:add']"
          @click="handleDelete(scope.row)"
          >删除</el-button
        >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改 -->
    <el-dialog title="修改资料" :visible.sync="open" width="30%" append-to-body v-loading="loading">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-row>
          <el-col :span="24">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="标题不能重复。自动去除空格。"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否公开" prop="fileType">
              <el-select v-model="form.isOpen" placeholder="默认公开">
                <el-option
                  v-for="item in this.openOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文件说明" prop="fileDesc">
              <el-input
                type="textarea"
                :rows="2"
                v-model="form.fileDesc"
                controls-position="right"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文件类型" prop="fileType">
              <el-select v-model="form.fileType" placeholder="选择类型">
                <el-option
                  v-for="item in this.dict.type.mc_file_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文件">
              <el-upload
                class="upload-demo"
                action="#"
                :limit="1"
                :show-file-list="true"
                ref="upload_projectVideo"
                :auto-upload="false"
                :http-request="function (file) {return submitProjectVideo(file)}"
                list-type="picture"
              >
                <el-button size="small" type="primary">点击上传</el-button>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import { MessageBox } from 'element-ui'
import { getOwnerFileInfo, delProductData } from '@/api/product/productInfoData'
import { uploadFuncAwait } from '@/utils/upload'

export default {
  name: "myPublic",
  props: {
    productTypeId: {
      type: String,
    },
  },
  dicts: ["review_status", "mc_file_type", "mc_product_type"],
  data() {
    return {

      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 表格数据
      tableData: [],
      projectVideoFile: '',
      // 是否显示弹出层
      open: false,
      openOptions: [{
        value: 1,
        label: '公开'
      }, {
        value: 0,
        label: '不公开'
      }],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productType: null,
        rangeTime: null,
        beginTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      baseURL: process.env.VUE_APP_DOWNLOAD_API,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询设备版本列表 */
    getList() {
      this.loading = true;
      getOwnerFileInfo(this.addDateRange(this.queryParams, this.dateRange)).then((response) => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        title : "",
        fileDesc: '',
        isOpen: 1,
        fileType: ''
      };
      this.productTypeId = '';
      this.projectVideoFile = "";
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    handleDownload(row){
      window.open(this.baseURL+ '/app/'+ row.url);
    },

    //删除视频教程
    handleDelete(row){
      // 通过的就不能再更改了
      if(1 != row.reviewState){
        MessageBox.confirm('此操作将永久删除该文件，是否继续？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delProductData({id:row.id}).then((res)=>{
            if(res.code == 200 ){
              this.$modal.msgSuccess("删除成功");
              this.getList()
            }
          })
        }).catch(() => {
          // 用户点击“取消”
          console.log("已取消");
        });
      }
      else{
        this.$modal.msgWarning("审核通过就不能再操作了");
      }
    },
    handleModify(row){
      // 通过的就不能再更改了
      if(1 != row.reviewState){
        this.reset();
        this.form.reviewId = row.reviewId;
        this.productTypeId = row.productType;
        this.open = true;
      }
      else{
        this.$modal.msgWarning("审核通过就不能再操作了");
      }
    },
    submitProjectVideo(file) {
      this.projectVideoFile = file.file;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          let postUrlImg = '/productDataInfo/modifyProductData'
          this.$refs.upload_projectVideo.submit()
          //if(this.projectVideoFile)
          {
            let formData = new FormData()
            formData.append('multipartFile', this.projectVideoFile)
            formData.append('title', this.form.title)
            formData.append('fileDesc', this.form.fileDesc)
            formData.append('fileType', this.form.fileType)
            formData.append('reviewId', this.form.reviewId)
            formData.append('productType', this.productTypeId)
            formData.append('isOpen', this.form.isOpen)
            this.loading = true
            await uploadFuncAwait(postUrlImg, formData).then((res)=>{
              if(res.data.code == 200){
                this.$modal.msgSuccess("修改成功，等待审核，审核结果在我的发布中查看");
                this.loading = false
                this.open = false
                this.getList()
              }else{
                this.$modal.msgError("修改失败: " + res.data.msg);
                this.loading = false
              }
            })
          }
        }
      });
    },
  },
};
</script>
<style scoped></style>
